// var DB = require("../lib/db_pg").DB;
var DB = require("../lib/db_mysql").DB;
var config = require('../lib/config');
var resErr = require('../lib/util.js').resErr;
var md5 = require('../lib/util.js').md5;
var async = require('async');
var http_request = require('../lib/util.js').http_request;

exports.location_list = function(req,res){
    var location_id = req.param('location_id');
    var sql_text = "SELECT location_id, location_name, dis_limit, detail_location, latitude, longitude, dt_am, dt_pm FROM t_location u WHERE 1=1";
    var tmp_params = [];
    var i=1;
    if(location_id) {
        sql_text += " and upper(location_id) like upper('" + location_id + "')";
    }
    
    var limit = parseInt(req.param("limit") || req.param("pageSize") ||'20');
    var page  = parseInt(req.param("page") || req.param("pageNum") || '1');  
    if(req.param("sort")){
        sql_text += " order by u." + req.param("sort");
        if(req.param("dir")){
            sql_text += " " + req.param("dir");
        }
    }
    
    var db = new DB();
    db.queryPage(sql_text,tmp_params,page,limit,function(err,result){
        if(err) return resErr(res, err);

        console.log(result);
        return res.json(result);
    });
};

exports.location_add = function(req, res){
    //var location_id = req.param('location_id');
    //if(!location_id) return res.json({success:false,msg:'院系代码不能为空！'});
    var location_name = req.param('location_name');
    if(!location_name) return res.json({success:false,msg:'地址名称不能为空'});
    var latitude = req.param('latitude');
    if(!latitude) return res.json({success:false,msg:'纬度不能为空'});
	var longitude = req.param('longitude');
    if(!longitude) return res.json({success:false,msg:'经度不能为空'});
    var dis_limit = req.param('dis_limit');
    if(!dis_limit) return res.json({success:false,msg:'最大距离不能为空'});
    var detail_location = req.param('detail_location');
    if(!detail_location) return res.json({success:false,msg:'详细地址不能为空'});
    var dt_am = req.param('dt_am');
    if(!dt_am) return res.json({success:false,msg:'签到时间不能为空'});
    var dt_pm = req.param('dt_pm');
    if(!dt_pm) return res.json({success:false,msg:'签退时间不能为空'});
    var reg = /^[0-9]{4}$/;
    if(!reg.test(dt_am)) return res.json({success:false,msg:'签到时间格式不对，格式为HHmm，例如0930'});
    if(!reg.test(dt_pm)) return res.json({success:false,msg:'签退时间格式不对，格式为HHmm，例如1730'});

    var db = new DB();
    
    async.waterfall([
        function(cb){
            var tmp_params = [];
            var sql_text = 'select count(1) from t_location where location_name = ?;';
            tmp_params.push(location_name);
            db.query(sql_text,tmp_params,function(err,datas){
                if(err) return cb(err);
                if(datas.length > 0 && datas[0].counter > 0){
                  return cb('该名称地址已经存在！'); // 不能重名
                }
                return cb(null);
            });
        }
    ], function(err){ // callback
        if(err) return resErr(res, err);
        var tmp_params = [];
        var sql_text = 'insert into t_location(location_name, latitude, longitude, dis_limit, detail_location, dt_am, dt_pm) values(?, ?, ?, ?, ?, ?, ?);';
        tmp_params.push(location_name);
        tmp_params.push(latitude);
        tmp_params.push(longitude);
        tmp_params.push(dis_limit);
        tmp_params.push(detail_location);
        tmp_params.push(dt_am);
        tmp_params.push(dt_pm);
        db.query(sql_text,tmp_params,function(err,datas){
            console.log(datas);
            if (err){
                return resErr(res,err);
            }      
            return res.json({success:true,msg:'实习地址添加成功！'});
        });
    });
};

exports.location_del = function(req, res){
    var location_id = req.param('location_id');
    if(!location_id) return res.json({success: false, msg: '地址代码不能为空'});
    
    var db = new DB();
    var sql_text = 'delete from t_location where location_id = ?;';
    
    var tmp_params = [];
    tmp_params.push(location_id);
    
    db.query(sql_text, tmp_params, function(err, datas){
        console.log(datas);
        if(err){
            return resErr(res, err);
        }
        return res.json({success: true, msg: '实习地址删除成功'});
    });
};

exports.location_upd = function(req, res){
	var location_id = req.param('location_id');
    if(!location_id) return res.json({success:false,msg:'地址代码不能为空'});
    var location_name = req.param('location_name');
    if(!location_name) return res.json({success:false,msg:'地址名称不能为空'});
    var latitude = req.param('latitude');
    if(!latitude) return res.json({success:false,msg:'纬度不能为空'});
	var longitude = req.param('longitude');
    if(!longitude) return res.json({success:false,msg:'经度不能为空'});
    var dis_limit = req.param('dis_limit');
    if(!dis_limit) return res.json({success:false,msg:'最大距离不能为空'});
    var detail_location = req.param('detail_location');
    if(!detail_location) return res.json({success:false,msg:'详细地址不能为空'});
    var dt_am = req.param('dt_am');
    if(!dt_am) return res.json({success:false,msg:'签到时间不能为空'});
    var dt_pm = req.param('dt_pm');
    if(!dt_pm) return res.json({success:false,msg:'签退时间不能为空'});

    var reg = /^[0-9]{4}$/;
    if(!reg.test(dt_am)) return res.json({success:false,msg:'签到时间格式不对，格式为HHmm，例如0930'});
    if(!reg.test(dt_pm)) return res.json({success:false,msg:'签退时间格式不对，格式为HHmm，例如1730'});
    
    var db = new DB();
    var sql_text = 'update t_location ' +
    			   '   set location_name = ?, ' +
    			   '       latitude = ?, ' +
    			   '       longitude = ?, ' +
    			   '       dis_limit = ?, ' +
                   '       detail_location = ?, ' +
                   '       dt_am = ?, ' +
                   '       dt_pm = ? ' +
    			   ' where location_id = ?;';
    
    var tmp_params = [];
    tmp_params.push(location_name);
    tmp_params.push(latitude);
    tmp_params.push(longitude);
	tmp_params.push(dis_limit);
    tmp_params.push(detail_location);
    tmp_params.push(dt_am);
    tmp_params.push(dt_pm);
    tmp_params.push(location_id);
    
    db.query(sql_text, tmp_params, function(err, datas){
        console.log(datas);
        if(err){
            return resErr(res, err);
        }
        return res.json({success: true, msg: '实习地址修改成功'});
    });
};
